/**
 * *****************************************************************************
 * @file    adt3102_flash.h
 * @author  Home(zhhuang@andartechs.com.cn)
 * @version 1.0.0
 * @date    2021-12-23
 * @brief   The W25X40CL Serial Flash memory,for related configuration operations,
 *          please check the data sheet.
 * 
 *                 Copyright (c) 2021, Andar Technologies Inc.
 *                           www.andartechs.com
 * 
 * *****************************************************************************
 */
#ifndef __ADT3102_FLASH_H
#define __ADT3102_FLASH_H
/*----------------------------- include --------------------------------------*/
#include "adt3102_type_define.h"


/*----------------------------- typedef --------------------------------------*/
typedef enum
{
    BUSY    = 0x01,
    WEL     = 0x02,
    BP0     = 0x04,
    BP1     = 0x08,
    BP2     = 0x10,
    TB      = 0x20,
    RV      = 0x40,
    SPR     = 0x80
} StateFgTypedef;


/*----------------------------- function -------------------------------------*/
/**
 * @brief   Reading ID from register.
 * @param   None.
 * @return  uint16.
 * @note
 */
uint16 ReadID(void);

/**
 * @brief   Erase full chip.
 * @param   None.
 * @return  None.
 * @note
 */
void ChipErase(void);

/**
 * @brief   Erase a sector of the chip. size: 4KB
 * @param   Dst_Addr. The sector address, set accroding to actual capacity.
 * @return  None.
 * @note    spiCsDeselect(SPI0) => spiCsSelect(SPI0)
 */
void sectorErase(uint32 Dst_Addr);

/**
 * @brief   Erase a block of the chip. size: 32KB
 * @param   None.
 * @return  None.
 * @note    spiCsDeselect(SPI0) => spiCsSelect(SPI0)
 */
void blockErase(uint32 Dst_Addr);

/**
 * @brief   SPI Wirites less than 256 bytes of data in a page(0 ~ 65535).
 *          Write a maximum of 256 bytes of data at the specified start.
 * @param   pBuffer: Data storage area.
 * @param   ReadAddr: Start reading address.(24-bit)
 * @param   NumByteToRead: The number of bytes to read. Max: 65535.
 * @return  None.
 * @note
 */
void Write_Page(uint8* pBuffer,uint32 WriteAddr,uint16 NumByteToWrite);

/**
 * @brief   Reading SPI of flash.
 * @param   pBuffer: Data storage area.
 * @param   ReadAddr: Start reading address.(24-bit)
 * @param   NumByteToRead: The number of bytes to read. Max: 65535.
 * @return  None.
 * @note
 */
void Flash_Read(uint8* pBuffer,uint32 ReadAddr,uint16 NumByteToRead);


/**
 * @brief   Test download whether sucessful.
 * @param   pArray: test array.
 * @param   pLen:   test len.
 * @return  None.
 * @note
 */
void Flash_ReadCheck(uint8* pBuffer, uint32 StartAddr, uint16 NumByteToWrite);


/**
 * @brief   Wirite SPI Flash have a parity.
 * @param   pBuffer: Data storage area.
 * @param   WriteAddr: Start writing address.(24-bit)
 * @param   NumByteToWrite: The number of bytes to write. Max: 25.
 * @return  None.
 * @note    This function has an erase operation!!!
 */
void Flash_Write(uint8* pBuffer, uint32 WriteAddr, uint16 NumByteToWrite);

#endif // !__ADT3102_FLASH_H
